package com.huli.exception;

import com.huli.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler()
    @ResponseBody
    public Result tryPrintException(Exception e) {
        if (e instanceof org.springframework.validation.BindException) {
            e.printStackTrace();
            return Result.failedResult(ExceptionEnum.ARGS_MISTAKE);
        } else if (e instanceof CustomException) {
            return ((CustomException) e).getResult();
        } else {
            log.error("------------------------->{}",e.toString());
            e.printStackTrace();
            return Result.failedResult(ExceptionEnum.SERVERS_ERROR);
        }
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = IllegalArgumentException.class)
    public Result handler(IllegalArgumentException e) {
        log.error("Assert异常：----------------{}", e.getMessage());
        return Result.failedResult(500,e.getMessage());

    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = AssertionError.class)
    public Result handler(AssertionError e) {
        log.error("Assert异常：----------------{}", e.getMessage());
        return Result.failedResult(500,e.getMessage());

    }

}